#include "LCD1602.h"

#include "assets.h"
#include "delay.h"

void LCD1602_Data(uint8_t d) {
	LCD1602_Port = d;
	LCD1602_EN = 1;
	LCD1602_EN = 0;
}

void LCD1602_Init(void) {
	LCD1602_CMDMode();
	LCD1602_WriteMode();
	LCD1602_Data(0x01);//Reset DDRAM
	delay(0.01 * CLOCK_PER_SEC,false);//10ms
	LCD1602_Data(0x38);//8-Bit, 2 Lines, 5x8
	LCD1602_Data(0x0C);//Display on, cursor off
}

void LCD1602_CreateChar(uint8_t addr, uint8_t code *graph) {
	uint8_t i;
	LCD1602_CMDMode();
	LCD1602_Data(0x40 | (addr << 3));//Select CGRAM
	LCD1602_DataMode();
	for (i = 0; i < 8; i++) {
		LCD1602_Data(graph[i]);
	}
}

//输出代码空间的字符串
void LCD1602_PutStr_c(char code *str) {
	char c;
	LCD1602_DataMode();
	while ((c = *str) != '\0') {
		LCD1602_Data(c);
		str++;
	}
}

//输出字符串
void LCD1602_PutStr(char *str) {
	char c;
	LCD1602_DataMode();
	while ((c = *str) != '\0') {
		LCD1602_Data(c);
		str++;
	}
}

//输出两行信息
void LCD1602_PrintAll(char code *str1, char code *str2) {
	LCD1602_DDRamAddr(0x00);
	LCD1602_PutStr_c(str1);
	LCD1602_PutStr_c(STR_Space);
	LCD1602_DDRamAddr(0x40);
	LCD1602_PutStr_c(str2);
	LCD1602_PutStr_c(STR_Space);
}
